package com.hcss.sys.version.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hcss.common.util.DateUtil;
import com.hcss.paging.service.SchemaPagingByExample;
import com.hcss.sys.version.constant.HcSysVersionConstants;
import com.hcss.sys.version.dao.HcSysVersionComplexMapper;
import com.hcss.sys.version.dao.HcSysVersionMapper;
import com.hcss.sys.version.model.HcSysVersion;
import com.hcss.sys.version.model.HcSysVersionExample;
import com.hcss.sys.version.model.par.HcSysVersionPar;
import com.hcss.sys.version.model.vo.HcSysVersionVO;
import com.hcss.sys.version.service.HcSysVersionService;
import com.hcss.util.EmptyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @描述: HcSysVersion-服务的实现类
 * @作者: fangtao
 * @创建日期: 2019-08-07 07:51:34
 */
@Service("hcSysVersionService")
public class HcSysVersionServiceImpl implements HcSysVersionService {
	
	@Autowired
	private HcSysVersionMapper hcSysVersionMapper;
	@Autowired
	private HcSysVersionComplexMapper hcSysVersionComplexMapper;

	@Override
	public PageInfo<HcSysVersion> queryPagingData(HcSysVersionPar par) throws Exception{
        HcSysVersionExample example = (HcSysVersionExample) SchemaPagingByExample.getBaseExample(HcSysVersionExample.class, par);
        PageHelper.startPage(par.getPage(), par.getPerpage());
        List<HcSysVersion> list =  hcSysVersionMapper.selectByExample(example);
        return new PageInfo<>(list);
	}
	
	@Override
	public List<HcSysVersion> queryAllData(HcSysVersionPar par) throws Exception {
		HcSysVersionExample example = (HcSysVersionExample) SchemaPagingByExample.getBaseExample(HcSysVersionExample.class, par);
		return hcSysVersionMapper.selectByExample(example);
	}

	@Override
	public void insertModel(HcSysVersion model) {
		hcSysVersionMapper.insertSelective(model);
	}

	@Override
	public void updateModel(HcSysVersion model) {
        HcSysVersionExample example = new HcSysVersionExample();
        // 补充更新条件
        hcSysVersionMapper.updateByExampleSelective(model, example);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		HcSysVersionExample example = new HcSysVersionExample();
		example.createCriteria().andIdIn(ids);
		hcSysVersionMapper.deleteByExample(example);
	}
	
	@Override
	public HcSysVersion selectByPK(String id) {
		if (EmptyUtil.isHaveEmpty(id)) return null;

        HcSysVersionExample example = new HcSysVersionExample();
        example.createCriteria().andIdEqualTo(id);
		List<HcSysVersion> list = hcSysVersionMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public HcSysVersionVO getVersionInfo (){
		HcSysVersionVO model=new HcSysVersionVO();
		model.setStatus(HcSysVersionConstants.STATUS.STATUS_01);
		List<HcSysVersionVO>list=hcSysVersionComplexMapper.selectVersionComplex(model);
		if(!CollectionUtils.isEmpty(list)){
			model.setHcSysVersionVOList(list);//更新内容的集合
			model.setId(list.get(0).getId());
			model.setVersionno(list.get(0).getVersionno());//版本
			model.setVersiontime(list.get(0).getVersiontime());//date类型更新日期
			model.setVersiontimestr(DateUtil.dateToString(model.getVersiontime(), DateUtil.FORMAT_yMdHms));//String类型更新日期
		}
		return model;
	}
}
